/**
 * 此文件用于发起群聊弹框的状态及数据管理
 */
import { ifContactInList } from '@/utils/socketTools'
import { createGroup, groupInfo, groupMemberList, groupMembersDelete, groupMembersAdd } from '@/api/group'

const state = {
  // 添加类型
  addType: 0, // 0:发起群聊   //1:私聊转群聊  //2:添加新成员入群 //3:添加参会人 //4:邀请入会  //5:只在群组邀请
  // 群内已有人员列表
  currentList: [], // 添加新成员入群用
  // 已选择人员列表
  selectedList: []

}

const mutations = {
  // 改变添加状态
  set_addType (state, type) {
    state.addType = type
  },
  // 设置当前群现有成员
  set_currentList (state, list) {
    state.currentList = list
  },
  // 设置已选择人员列表
  set_selectedList (state, list) {
    state.selectedList = list
  },
  // 选择成员
  add_selectUser (state, user) {
    if (state.selectedList.every(item => item.user_id !== user.user_id)) {
      state.selectedList.push(user)
    }
  },
  // 取消选择
  delete_selectUser (state, userId) {
    state.selectedList = state.selectedList.filter(item => item.user_id !== userId)
  }
}

const actions = {
  // 创建群聊
  createGroupChat ({ state, rootState, commit }, groupName) {
    const groupMembers = state.selectedList.map(item => {
      return item.user_id
    })
    if (state.addType === 1) {
      groupMembers.unshift(rootState.chatInformation.current_contact.id)
    }
    groupMembers.unshift(rootState.user.userInfo.user_id)
    return new Promise((resolve, reject) => {
      createGroup(groupMembers, groupName).then(res => {
        if (res.data && res.data.code === 200) {
          commit('chatInformation/add_group', {
            groupID: res.data.data.group_id,
            groupInfo: {
              group_name: res.data.data.group_name,
              group_sponsor: res.data.data.group_sponsor,
              newCreate: true
            }
          }, { root: true })
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取群聊信息
  getGroupInfo (context, groupID) {
    return new Promise((resolve, reject) => {
      groupInfo(groupID).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 群聊人员列表
  getGroupMemberList ({ rootState, commit }, groupID) {
    return new Promise((resolve, reject) => {
      groupMemberList(groupID).then(async res => {
        if (res.data && res.data.code === 200) {
          const arr = []
          res.data.data.forEach(async item => {
            await ifContactInList(item.user_id, 1)
            item.user_name = rootState.chatInformation.userList[item.user_id].user_name
            item.remarks = rootState.chatInformation.userList[item.user_id].remarks ? rootState.chatInformation.userList[item.user_id].remarks : ''
            if (item.user_id === rootState.chatInformation.groupList[groupID].group_sponsor) {
              arr.unshift(item)
            } else {
              arr.push(item)
            }
            commit('set_currentList', arr)
          })
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 踢出群成员
  deleteMembers (context, { groupID, groupMembers }) {
    return new Promise((resolve, reject) => {
      groupMembersDelete(groupID, groupMembers).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 添加群成员
  addMembers ({ context }, groupID) {
    const groupMembers = state.selectedList.map(item => {
      return item.user_id
    })
    return new Promise((resolve, reject) => {
      groupMembersAdd(groupID, groupMembers).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
